﻿var ns, toast, imageBrowser, docReader;
var app = new Vue({
    el: '#app',
    data: {
        ID: 0,
        IDs: 0,
        IsRead: false,//当前登录人是否已读还是未读   
        TypeStatus: 0,//0 通知我的  1 我发起的
        form: {
            Title: '',
            Content: '',
            Name: '',
            AddTime: '',
            States: 1,//1暂存  2 已发布
        },
        ReadList: [],//已读列表
        NoReadList: [],//未读列表
        imglist: [
            // { local_id: 'http://demo.saasyy.com/upload/ZhuangXiu/20200904/132436810283341358.jpg', media_id: '0',},
            // { local_id: 'http://demo.saasyy.com/upload/ZhuangXiu/20200904/132436810283341358.jpg', media_id: '1',},    
        ],//图片信息
        filelist: [
            // { SysFinalFilePath: 'http://demo.saasyy.com/upload/ZhuangXiu/20200904/132436810283341358.jpg', media_id: '2',FileOriNames:'个人转正申请表.doc', },
            // { SysFinalFilePath: 'http://demo.saasyy.com/upload/ZhuangXiu/20200904/132436810283341358.jpg', media_id: '3',FileOriNames:'个人工作记录汇总表.xlxs', },
        ],//附件信息
        IsReadedCheck: true,//控制已读未读切换显示
        previewImage: [],
        IsReplay: false,//是否弹出回复功能
        ReplayContent: '',//回复内容
        IsShowEdit: false,
    },
    methods: {
        do_more_neibu: function () {
            var that = this;
            if(that.form.States==0){
                return;
            }
            that.IsShowEdit = !that.IsShowEdit;
        },
        get_data: function () {
            var that = this;
            var options = {};
            options.ID = that.ID;
            options.action = "getinternalnotificationdetails";
            toast.loading({
                title: "加载中",
                duration: 2000
            }, function (ret) { });
            ns.post(options, function (succeed, data, err) {
                toast.hide();
                if (succeed) {
                    that.form = data.newModel;
                    that.filelist = data.ImgList;//ImgList（SysFinalFilePath路径 FileOriNames：名字）
                    that.ReadList = data.RaedList;
                    that.NoReadList = data.UnreadList;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        img_browser: function (imgurl) {
            var that = this;
            var imgurls = [];
            imgurls.push(imgurl);
            imageBrowser.openImages({
                imageUrls: imgurls,
                showList: false,
                activeIndex: 0
            });
        },
        doPreviewFile: function (item) {
            var that = this;
            var i = item.FileOriNames.lastIndexOf('.');
            var j = item.FileOriNames.length;
            var nameType= item.FileOriNames.slice(i + 1, j);
            if(nameType=='png'||nameType=='jpg'||nameType=='tif'||nameType=='gif'){
                that.img_browser(item.SysFinalFilePath);
            }else{
                that.do_openFile(item);
            }
        },
        img_browser: function(imgurl) {
            var that = this;
            var imgurls = [];
            imgurls.push(imgurl);
            imageBrowser.openImages({
                imageUrls: imgurls,
                showList: false,
                activeIndex: 0
            });
        },

        do_openFile:function(item){
            api.download({
                url: item.SysFinalFilePath,
                savePath: 'fs://app/' + item.FileOriNames,
                report: true,
                cache: true,
                allowResume: true
            }, function (ret, err) {
                if (ret.state == 1) {
                    docReader.open({
                        path: ret.savePath,
                        autorotation: false
                    }, function(ret, err) {
                        if (ret.status) {
                            // alert(JSON.stringify(ret));
                        } else {
                            alert(JSON.stringify(err));
                        }
                    });
                }else{
                    api.toast({
                        msg: '打开失败',
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        doChangeRead: function (value) {
            this.IsReadedCheck = value;
        },
        showPopup: function () {
            this.show = true;
        },
        //回复
        doReplay: function () {
            this.IsReplay = true;
        },
        //确认收到
        do_getok: function (type) {
            var that = this;
            //调用接口
            var options = {};
            options.IDs = that.IDs;
            options.tdRemark = type == 0 ? '收到' : that.ReplayContent;
            options.action = "savanotificationread";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        if (type == 1) {
                            this.IsReplay = false;
                        }
                        //刷新并关闭
                        setTimeout(() => {
                            api.sendEvent({
                                name: 'reloadneibulist',
                            });
                            api.closeWin();
                        }, 300);
                        return;
                    }
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        //发布
        do_release: function (item) {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '确定发布该内部通知',
                buttons: ['确定', '取消']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        //调用接口
                        var options = {};
                        options.ID = that.ID;
                        options.action = "publishinternalnotification";
                        ns.post(options, function (succeed, data, err) {
                            if (succeed) {
                                if (data.status) {
                                    api.toast({
                                        msg: '操作成功',
                                        duration: 2000,
                                        location: 'bottom'
                                    });
                                    //刷新并关闭
                                    setTimeout(() => {
                                        api.sendEvent({
                                            name: 'reloadneibulist',
                                        });
                                        that.get_data();
                                    }, 300);

                                    return;
                                }
                                api.toast({
                                    msg: data.Msg,
                                    duration: 2000,
                                    location: 'bottom'
                                });

                            } else if (err) {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        });
                    };
                };
            });
        },
        //撤回
        do_recall: function (item) {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '撤回通知后，通知对象无法查看本条通知的内容，确定要撤回吗？',
                buttons: ['确定', '取消']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        //调用接口
                        var options = {};
                        options.ID = that.ID;
                        options.action = "readwithdrawal";
                        ns.post(options, function (succeed, data, err) {
                            if (succeed) {
                                if (data.status) {
                                    api.toast({
                                        msg: '操作成功',
                                        duration: 2000,
                                        location: 'bottom'
                                    });
                                    //刷新并关闭
                                    setTimeout(() => {
                                        api.sendEvent({
                                            name: 'reloadneibulist',
                                        });
                                        that.get_data();
                                    }, 300);

                                    return;
                                }
                                api.toast({
                                    msg: data.Msg,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            } else if (err) {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        });
                    };
                };
            });
        },
        //修改内部通知
        do_edit_neibu: function () {
            var that = this;
            that.IsShowEdit=false;
            ns.openWin('neibutz_add', '编辑内部通知', {
                id: that.ID
            });
        },
        //删除
        do_delete: function () {
            var that = this;
            that.IsShowEdit=false;
            api.confirm({
                title: '提示',
                msg: '确定要删除本条通知信息吗？',
                buttons: ['确定', '取消']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        //调用接口 api.closeWin();
                        var options = {};
                        options.ID = that.ID;
                        options.action = "deleteinternal";
                        ns.post(options, function (succeed, data, err) {
                            if (succeed) {
                                if (data.status) {
                                    api.toast({
                                        msg: '操作成功',
                                        duration: 2000,
                                        location: 'bottom'
                                    });
                                    //刷新并关闭
                                    setTimeout(() => {
                                        api.sendEvent({
                                            name: 'reloadneibulist',
                                        });
                                        api.closeWin();
                                    }, 300);
                                    return;
                                }
                                api.toast({
                                    msg: data.Msg,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            } else if (err) {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        });

                    };
                };
            });
        },
    }
});

apiready = function () {
    api.parseTapmode();
    imageBrowser = api.require('imageBrowser');
    docReader = api.require('docReader');

    ns = window.Foresight.Util;
    toast = new auiToast();

    app.ID = api.pageParam.id || 0;
    app.IDs = api.pageParam.ids || 0;
    app.IsRead = api.pageParam.IsRead || false;
    app.TypeStatus = api.pageParam.TypeStatus || 0;
    app.get_data();

    api.addEventListener({
        name: 'do_more_neibu'
    }, function (ret, err) {
        //判断点击后是否有编辑、删除权限
        app.do_more_neibu();
    });

    api.addEventListener({
        name: 'do_reload_neibu'
    }, function (ret, err) {
        //判断点击后是否有编辑、删除权限
        app.get_data();
    });

    //点击查看图片
    setTimeout(function () {
        var imageshow = document.getElementsByTagName('img');
        if (imageshow.length > 0) {
            for (var i = 0; i < imageshow.length; i++) {
                imageshow[i].onclick = function (ret) {
                    app.img_browser(this.src);
                }
            }
        }
    }, 500);
};